package com.chw.shop.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.chw.common.diy.CommonData;
import com.chw.common.diy.CountException;
import com.chw.common.exception.ParamExcption;
import com.chw.shop.dao.ISpecsDao;
import com.chw.shop.dao.ShopDao;
import com.chw.shop.entity.po.CarData;
import com.chw.shop.entity.po.Shop;
import com.chw.shop.entity.po.ShopSpecs;
import com.chw.shop.entity.vo.ShopImgVo;
import com.chw.shop.service.CarService;
import com.chw.user.entity.po.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Service
public class CarSeriviceImpl implements CarService {
    @Autowired
    private ShopDao shopDao;
    @Autowired
    private ISpecsDao iSpecsDao;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public void addCarInfo(Integer id, Integer sid, Integer count) {
        if (id==null || count==null){
            throw new ParamExcption();
        }
        Shop shop =  shopDao.selectById(id);
        //验库存
        Integer total=0;
        Double price=0D;
        //验证是否为sku属性
        if (sid!=null){
            ShopSpecs specs = iSpecsDao.selectById(sid);
            //转换成json字符串
            JSONObject parse = (JSONObject) JSONObject.parse(specs.getValuess());
            total=parse.getInteger("count");
            price=parse.getDouble("price");
        }else {//不是sku属性
            total=shop.getCount();
            price=shop.getPrice();
        }
        if (total<count){
            throw new CountException("库存不足 剩余"+total);
        }
        //构造数据
        CarData cd=new CarData();
        cd.setId(id);
        cd.setSid(sid);
        cd.setCount(count);
        cd.setPrice(price);
        cd.setImg(shop.getImg());
        cd.setTitle(shop.getTitle());
        //取出购物车数据
        //得到用户唯一标识
        User u= (User) request.getAttribute("u");
        List<CarData> carData = (List<CarData>) redisTemplate.opsForHash().get(CommonData.REDIS_CAR_INFO_ID + CommonData.PRO, u.getName());
        if (carData==null){
            //第一次添加商品
            carData=new ArrayList<>();
            carData.add(cd);
        }else {
            boolean flag=false;
            for (CarData c:carData) {//遍历 判断此商品是否是购物车中已有的商品
                if (id.equals(c.getId())){
                    if (sid==null){
                        //判断是否是sku属性
                        flag=true;
                        //购物车中有此商品
                        if (c.getCount()+count>total){
                            throw new CountException("库存不足 剩余"+total);
                        }
                        c.setCount(c.getCount()+count);
                        break;
                    }else {
                        //是sku
                        if(sid.equals(c.getSid())){
                            //是同一件商品
                            flag=true;
                            if (c.getCount()+count>total){
                                throw new CountException("库存不足 剩余"+total);
                            }
                            c.setCount(c.getCount()+count);
                            break;
                        }
                    }
                }

            }
            if (flag==false){
                //购物车中没有添加这件商品
                carData.add(cd);
            }
        }
        redisTemplate.opsForHash().put(CommonData.REDIS_CAR_INFO_ID+CommonData.PRO,u.getName(),carData);
    }
}
